package cate

import (
	"encoding/json"
	"fmt"
	cateServe "forum/app/service/cate"
	cateData "forum/app/structData/cate"
	"forum/app/tools"
	"net/http"
)

// 定义请求参数结构体

func GetList(w http.ResponseWriter, r *http.Request) {
	var requestData cateData.GetListRequest
	json.NewDecoder(r.Body).Decode(&requestData)

	tools.TryCatch(func() {
		list := cateServe.GetList(requestData.KeyWord)
		if list == nil {
			list = make([]map[string]interface{}, 0) // 根据实际类型调整
		}
		tools.OutPutSuccess(w, list)
	}, func(a any) {

	})
}

func Create(w http.ResponseWriter, r *http.Request) {
	var requestData cateData.CreateRequest
	json.NewDecoder(r.Body).Decode(&requestData)

	fmt.Println(requestData)
	tools.TryCatch(func() {
		cateServe.Create(requestData)
		tools.OutPutSuccess(w, "创建成功")
	}, func(err any) {
		tools.OutPutError(w, fmt.Sprint(err))
	})
}
